package com.example.oms.controller;

import com.example.oms.entity.Patients;
import com.example.oms.service.CashierService;
import com.example.oms.utils.DataResult;
import com.example.oms.vo.PayItem;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("cashier")
public class CashierController {

    @Resource
    CashierService cashierService;

    //按病历id检索所有状态为已付款的项目
    @PostMapping("/getAllPaidByDiagId")
    public DataResult getAllPaidByDiagId(@RequestBody Map<String,String> map){
        int diagId = Integer.parseInt(map.get("diagId"));
        List<PayItem> payItemList = cashierService.getAllPaidByDiagId(diagId);
        return new DataResult(200,payItemList,"查询成功");
    }

    //按照病历id退费
    @PostMapping("/refund")
    public DataResult refund(@RequestBody Map<String,String> map){
        int diagId = Integer.parseInt(map.get("id"));
        String category = map.get("category");
        int chargeCancel = cashierService.chargeCancel(diagId,category);
        return new DataResult(200,chargeCancel,"退费成功");
    }

    //按病历ID获取未收费项目
    @PostMapping("/getAllUnpaid")
    public DataResult getAllUnpaid(@RequestBody Map<String,String> map){
        int diagId = Integer.parseInt(map.get("diagId"));
        Map<String,Object> res = new HashMap<>();
        Patients patient = cashierService.getPatientByDiagId(diagId);
        List<PayItem> payItemList = cashierService.getAllUnpaidByDiagId(diagId);
        res.put("patient",patient);
        res.put("items",payItemList);
        return new DataResult(200,res,"查询成功");
    }

    //根据病历id收费
    @PostMapping("/chargeConfirmByDiagId")
    public DataResult chargeConfirmByDiagId(@RequestBody Map<String,String> map){
        int diagId = Integer.valueOf(map.get("diagId"));
        int chargeConfirmCount = cashierService.chargeConfirm(diagId);
        if(chargeConfirmCount == 0){
            return new DataResult(201,null,"未检测到收费项目");
        }
        return new DataResult(200,chargeConfirmCount,"收费成功");
    }
}
